import {
  reqAddOrUpdateCart,
  reqShopcartList,
  reqChangeChecked,
  reqAllChecked,
  deleteShopCart,
  deleteChooseCart,
} from '@/api'
export default {
  state: {
    ShopcartList: [],
  },
  mutations: {
    UPDATE_SHOPCARTLIST(state, ShopcartList) {
      state.ShopcartList = ShopcartList
    },
  },
  actions: {
    async getAddOrUpdateCart({ commit }, { skuId, skuNum }) {
      let result = await reqAddOrUpdateCart(skuId, skuNum)
      if (result.code === 200) {
        //后端成功添加到购物车
        return 'ok'
      } else {
        //后端没添加到购物车
        return Promise.reject('fail')
      }
    },
    async getShopcartList({ commit }) {
      let result = await reqShopcartList()
      if (result.code === 200) {
        commit('UPDATE_SHOPCARTLIST', result.data[0]?.cartInfoList)
      }
    },
    async getChangeChecked({ commit }, { skuId, isChecked }) {
      let result = await reqChangeChecked(skuId, isChecked)
      if (result.code === 200) {
        return 'ok'
      } else {
        return Promise.reject('fail')
      }
    },
    async changeAllChecked({ commit }, { isChecked, skuIdList }) {
      let result = await reqAllChecked(isChecked, skuIdList)
      if (result.code === 200) {
        return 'ok'
      } else {
        return Promise.reject('fail')
      }
    },
    async reqDeleteShopCart({ commit }, skuId) {
      let result = await deleteShopCart(skuId)
      if (result.code === 200) {
        return 'ok'
      } else {
        return Promise.reject('fail')
      }
    },
    async reqDeleteAllCart({ commit }, skuIdList) {
      let result = await deleteChooseCart(skuIdList)
      if (result.code === 200) {
        return 'ok'
      } else {
        return Promise.reject('fail')
      }
    },
  },
  getters: {},
}
